#!/usr/bin/python2.4
#
# Copyright 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


"""spiral's bulk uploader module to Google App Engine.

This is spiral bulk uploader that reads CSV to be imported into GAE datastore.
"""

import logging
from google.appengine.api import datastore_types
from google.appengine.ext import bulkload
from google.appengine.ext import search

__author__ = 'stratus@google.com (Gustavo Franco)'

PKGKIND = 'Package'


class PackageLoader(bulkload.Loader):
  """PackageLoader extends bulkload.Loader to index data while it uploads."""

  def __init__(self):
    """Invoke bulkload.Loader to prepare data for upload."""

    bulkload.Loader.__init__(self, PKGKIND,
                             [('name', str),
                              ('description', datastore_types.Text),
                              ('version', str),
                              ('architecture', str),
                              ('maintainer', unicode),
                              ('source', str),
                              ('priority', str),
                              ('section', str),
                              ('filename', str),
                              ('votes', int),
                              ('contents', datastore_types.Text),
                              ('mirror', str),
                              ('dist', str),
                              ('component', str),
                              ('repositoryarch', str)])

  def HandleEntity(self, entity):
    """HandleEntity overridden to index data right before it is uploaded.

    Args:
     entity: a set of data (Package) to be indexed

    Returns:
     indexed_entity: the same set of data plus an index
    """

    logging.debug('CSV content parsed, now indexing ' + entity['name'])
    indexed_entity = search.SearchableEntity(entity)
    return indexed_entity

if __name__ == '__main__':
  bulkload.main(PackageLoader())
